﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Bridges;
using System.Windows.Forms;

namespace Krile.Forms.MainForm.Controls.XTab
{
    public class XTabWindowItem : XTabItem
    {
        public bool Locked = false;
        public Form LinkedForm { get; set; }
        public XTabWindowItem(Form link, XTab par, string n)
            : base(par, n, Properties.Resources.showcascade)
        {
            link.FormClosed += new FormClosedEventHandler(link_FormClosed);
            link.Enter += new EventHandler(link_Enter);
            //link.Activated += new EventHandler(link_Activated);
            this.LinkedForm = link;
            InitPopupMenu();
        }

        public XTabWindowItem(Form link, XTab par, string n, bool locked)
            : this(link, par, n)
        {
            this.Locked = locked;
            toggleLock.Checked = this.Locked;
            UpdateIcon();
        }


        void link_Enter(object sender, EventArgs e)
        {
            //フォームのタイトルバーをクリックしたときのEnter/Activateイベントに
            //Refresh等の処理が入っていると問答無用で最大化されるバグがあるので,
            //一旦別スレッドを立ち上げてそっちで処理待ちしてもらう.
            var subt = new Action(() => parent.SafeOperate(() => parent.SelectedItem = this));
            subt.BeginInvoke((iar) => ((Action)iar.AsyncState).EndInvoke(iar), subt);
        }

        public override string Name
        {
            get
            {
                if (LinkedForm == null)
                    return "[NULL]";
                else
                    return LinkedForm.Text;
            }
            set { }
        }

        void link_FormClosed(object sender, FormClosedEventArgs e)
        {
            base.CloseThis();
        }

        public override void OnSelected()
        {
            LinkedForm.Activate();
            base.OnSelected();
        }

        protected override void CloseThis()
        {
            LinkedForm.Close();
        }

        ContextMenuStrip cms = null;
        ToolStripMenuItem toggleLock = null;
        private void InitPopupMenu()
        {
            cms = new ContextMenuStrip();

            toggleLock = new ToolStripMenuItem(Lang.XTab.XTIPop_Lock, Properties.Resources.locked, new EventHandler(ToggleLock));
            cms.Items.Add(toggleLock);

            cms.Items.Add(Lang.XTab.XTIPop_Close, Properties.Resources.tab_delete, new EventHandler(CloseThis));
        }

        public override void OnMouseClick(object obj, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                cms.Show(parent, e.Location);
            else
                base.OnMouseClick(obj, e);
        }

        protected override bool IsLocked
        {
            get
            {
                return this.Locked;
            }
        }

        internal void ToggleLock(object sender, EventArgs e)
        {
            this.Locked = !this.Locked;
            toggleLock.Checked = this.Locked;
            UpdateIcon();
            Refresh();
        }

        internal void CloseThis(object obj, EventArgs e)
        {
            this.CloseThis();
        }

        public void UpdateIcon()
        {
            if (this.Locked)
                this.IconImage = Properties.Resources.locked;
            else
                this.IconImage = Properties.Resources.showcascade;
            parent.Refresh();
        }

    
    }
}
